﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Yugi.Client.Card;
using Yugi.Client.Card.FileDecoder;
namespace Yugi.Client.Converter
{
    partial class ConvertData
    {
        private void ConvertData_RPS_Draw(byte[] input)
        {

        }

        private void ConvertData_RPS_CanClick(byte[] input)
        {

        }

        private void ConvertData_RPS_Hand(byte[] input)
        {
            Result.Add((int)input[0]);
            Result.Add((int)input[1]);
        }

        private void ConvertData_RPS_Chose(byte[] input)
        {
            Result.Add((int)input[0]);
            Result.Add((int)input[1]);
        }

        private void ConvertData_RPS_Finish(byte[] input)
        {

        }

        private void ConvertData_GAME_BEGIN(byte[] input)
        {
          
                Result.Add((int)input[0]);
                Result.Add((int)input[1]);
          
         
        }
        private void ConvertData_GAME_END(byte[] input)
        {

            Result.Add((int)input[0]);
            Result.Add((int)input[1]);


        }
        private void ConvertData_GAME_CHIABAI(byte[] input)
        {
                YBaseCard card;
                for (int i = 0; i < 10; i+=2)
                {
                    //int type_id=(int)input[i];
                    //int card_id = (int)input[i + 1];
                    int type_id = DecodeInt(ref input,0);
                    int card_id = DecodeByte(ref input, 0);
                    card = GetCard(type_id);
                    card.ID = card_id;
                    Result.Add(card);
                }
           
           
        }
        private void ConvertData_CARD_RUTBAI_DOITHU(byte[] input)
        {


        }
        private void ConvertData_CARD_XOABAI_DOITHU_TAY(byte[] input)
        {


        }
        private void ConvertData_GAME_RESET(byte[] input)
        {
            
            
        }
        private void ConvertData_CARD_INDECK(byte[] input)
        {
            //int number = (int)input[0];
            int number = DecodeByte(ref input, 0);
            int type_id;
            int id;
            YBaseCard card;
            Result.Add(number);
            for(int i=0;i<number*2;i+=2)
            {
                //type_id = input[i+1];
                //id = input[i + 2];
                type_id = DecodeInt(ref input);
                id = DecodeByte(ref input, 0);
                card = GetCard(type_id);
                card.ID = id;
                Result.Add(card);
            }
            id = 0;
        } 
        //------------------------Send----------------------------//

       
        private void ConvertSendData_RPS_Hand(object[] input)
        {
            Result_Send.Add((byte)(int)input[0]);
            Result_Send.Add((byte)(int)input[1]);
        }

        private void ConvertSendData_RPS_Chose(object[] input)
        {
            Result_Send.Add((byte)(int)input[0]);
            Result_Send.Add((byte)(int)input[1]);
        }

        private void ConvertSendData_RPS_Draw(object[] input)
        {

        }

        private void ConvertSendData_RPS_Finish(object[] input)
        {

        }
        private void ConvertSendData_GAME_CHIABAIXONG(object[] input)
        {

        }
        private void ConvertSendData_PLAYER_LOGOUT(object[] input)
        {

        }
        private void ConvertSendData_PLAYER_LOGIN(object[] input)
        {
            Result_Send.Add((byte)(int)input[0]);
        }
        //--------------------------------------------------------//
        static Dictionary<int, System.Windows.Media.ImageSource> Db = new Dictionary<int, System.Windows.Media.ImageSource>();
        internal YBaseCard GetCard(int type_id)
        {
            
            
            string path = Card.Load.PathManager.DataRoot;
            YFileDecoder decoder = new YFileDecoder();

            System.IO.FileInfo file = new System.IO.FileInfo(path + "\\_" + type_id.ToString() + ".bin");
            if (file.Exists==false)
            {
                //while (!try_connect)
                //{
                //    System.Threading.Thread.Sleep(200);
                //}
                //    if (dowload_file != null)
                //        dowload_file.SendRequest(type_id);
                
                
            }
            YBaseCard card= decoder.BinaryFileDecoderCard(path + "\\_" + type_id.ToString() + ".bin");
            card.Card_TypeID = type_id;
            if(Db.ContainsKey(type_id))
            {
                card.displayImage = null;
                card.displayImage = Db[type_id];
            }
            return card;
        }

        //---------------------------------------------------------------------------//

        internal int DecodeInt( ref byte[] input,int begin)
        {
            int res= BitConverter.ToInt32(input,begin);
            var temp = input.ToList();
            temp.RemoveRange(begin, 4);
            input = temp.ToArray();
            return res;
        }

        internal int DecodeInt(ref byte[] input)
        {
            int res = DecodeInt(ref input, 0);
            return res;
        }

        internal int DecodeByte(ref byte[] input, int begin)
        {
            int res = input[begin];
            var temp = input.ToList();
            temp.RemoveAt(begin);
            input = temp.ToArray();
            return res;
        }

        internal int DecodeByte(ref byte[] input)
        {
            int res = DecodeByte(ref input, 0);
            return res;
        }
    }
}
